using System;
using System.Data;
using System.Windows.Forms;

namespace Glpt.Common
{
    /// <summary>
    /// 自定义的月份控件
    /// </summary>
    public partial class MonthControl : UserControl
    {
        /// <summary>
        /// SelectedIndexChanged事件
        /// </summary>
        public event EventHandler SelectedIndexChanged;
        private void MonthControl_SelectedIndexChanged(object sender, EventArgs e) { }  //添加方法
        /// <summary>
        /// 开始月
        /// </summary>
        public int BeginMonth = 1;
        /// <summary>
        /// 开始年份
        /// </summary>
        public int BeginYear = 1990;
        /// <summary>
        /// 结束年份
        /// </summary>
        public int EndYear = DateTime.Now.Year;
        private bool isInit = false;

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public MonthControl()
        {
            InitializeComponent();
            SelectedIndexChanged += new EventHandler(MonthControl_SelectedIndexChanged);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            if (isInit) return;
            cmbYear.ValueMember = "value";
            cmbYear.DisplayMember = "text";
            cmbYear.DataSource = GetYearDataSource(BeginYear, EndYear);
            cmbMonth.ValueMember = "value";
            cmbMonth.DisplayMember = "text";
            cmbMonth.DataSource = MonthDataSource;
        }

        /// <summary>
        /// 获取年份数据源
        /// </summary>
        /// <param name="ibegin">开始年份</param>
        /// <param name="iend">结束年份</param>
        /// <returns>年份列表</returns>
        public static DataView GetYearDataSource(int ibegin, int iend)
        {
            DataTable dtYearDataSource = new DataTable();
            dtYearDataSource.Columns.Add("value");
            dtYearDataSource.Columns.Add("text");
            while (ibegin <= iend)
            {
                dtYearDataSource.Rows.Add(new object[] { ibegin.ToString(), ibegin.ToString() + "年" });
                ibegin++;
            }
            dtYearDataSource.AcceptChanges();
            return dtYearDataSource.DefaultView;
        }

        /// <summary>
        /// 获取年份数据源
        /// </summary>
        public DataView MonthDataSource
        {
            get
            {
                DataTable dtMonthDataSource = new DataTable();
                dtMonthDataSource.Columns.Add("value");
                dtMonthDataSource.Columns.Add("text");
                for (int i = BeginMonth; i <= 12; i++)
                {
                    dtMonthDataSource.Rows.Add(new object[] { i.ToString().PadLeft(2, '0'), i.ToString() + "月" });
                }
                dtMonthDataSource.AcceptChanges();
                return dtMonthDataSource.DefaultView;
            }
        }

        /// <summary>
        /// 获取或设置月份值
        /// </summary>
        public string MonthValue
        {
            get
            {
                if (cmbYear.SelectedValue != null && cmbMonth.SelectedValue != null)
                    return cmbYear.SelectedValue.ToString() + cmbMonth.SelectedValue.ToString();
                else
                    return "";
            }
            set
            {
                if (!isInit)
                {
                    Init();
                    isInit = true;
                }
                string svalue = value;
                if (value.Length != 6 || !value.EndsWith("00"))
                {
                    svalue = Funcs.StrToDataTime(value).ToString("yyyyMM");
                }
                cmbYear.SelectedValue = svalue.Substring(0, 4);
                cmbMonth.SelectedValue = svalue.Substring(4, 2);
                if (cmbMonth.SelectedIndex < 0) cmbMonth.SelectedIndex = 0;
            }
        }

        private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(sender, e);
        }

        private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(sender, e);
        }
    }
}
